package com.zhou.dao.impl;

import com.zhou.bean.FastMan;
import com.zhou.dao.FastManDao;
import com.zhou.utils.DateFormatUtil;
import com.zhou.utils.DbUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FastManDaoImpl extends DbUtils implements FastManDao {

    @Override
    public List findAllAndAllDay() {
            String sql = "SELECT count(*) total,COUNT(to_days(createtime)=to_days(now()) or null) day FROM fastman";
            ResultSet query = query(sql, null);
            List list = new ArrayList<>();
            try {
                while (query.next()){
                    list.add(query.getInt("total"));
                    list.add(query.getInt("day"));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeAll();
            }
            return list;
    }

    /**
     * 查询快递员列表密码记得隐藏
     *
     * @return
     */
    @Override
    public List<FastMan> selectAll(Integer pageIndex,Integer pageSize) {
        String sql = "select id,username,sysphone,idcard,countexpress,createtime,logintime from fastman limit ?,? ";
        List param = new ArrayList();
        param.add(pageIndex);
        param.add(pageSize);
        List<FastMan> fastManList = new ArrayList<>();
        ResultSet query = query(sql, param);
        try {
            while (query.next()){
                FastMan fastMan = new FastMan();
                fastMan.setId(query.getInt("id"));
                fastMan.setUsername(query.getString("username"));
                fastMan.setSysPhone(query.getString("sysphone"));
                fastMan.setIdCard(query.getString("idcard"));
                fastMan.setCountExpress(query.getInt("countexpress"));
                fastMan.setCreateTime(DateFormatUtil.format(query.getTimestamp("createtime")));
                fastMan.setLoginTime(DateFormatUtil.format(query.getTimestamp("logintime")));
                fastManList.add(fastMan);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return fastManList;
    }

    /**
     * 查询总条数
     *
     * @return
     */
    @Override
    public int total() {
        int total = 0;
        String sql = "select count(*) from fastman";
        ResultSet query = query(sql, null);
        try {
            while (query.next()){
                total = query.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    /**
     * 增加快递员
     *
     * @param fastMan 快递员对象
     * @return
     */
    @Override
    public int addFastMan(FastMan fastMan) {
        String sql  = "insert into fastman(username,sysphone,idcard,password,createtime) values(?,?,?,?,?)";
        List list = new ArrayList();
        list.add(fastMan.getUsername());
        list.add(fastMan.getSysPhone());
        list.add(fastMan.getIdCard());
        list.add(fastMan.getPassword());
        list.add(new Date());
        int update = 0;
        try {
            update = update(sql, list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    /**
     * 通过手机号查询快递员
     *
     * @param sysPhone 快递员手机号
     * @return
     */
    @Override
    public FastMan findBySysPhone(String sysPhone) {
        String sql  = "select id,username,sysphone,idcard,password from fastman where sysphone=?";
        List param = new ArrayList();
        param.add(sysPhone);
        ResultSet query = query(sql, param);
        FastMan fastMan = new FastMan();
        try {
            while (query.next()){
                fastMan.setId(query.getInt("id"));
                fastMan.setUsername(query.getString("username"));
                fastMan.setSysPhone(query.getString("sysphone"));
                fastMan.setIdCard(query.getString("idcard"));
                fastMan.setPassword(query.getString("password"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return fastMan;
    }

    /**
     * 删除快递员
     *
     * @param sysPhone 快递员手机号
     * @return
     */
    @Override
    public int deleteBySysPhone(String sysPhone) {
        String sql = " delete from fastman where sysphone = ?";
        List param = new ArrayList();
        param.add(sysPhone);
        int update = 0;
        try {
            update = update(sql, param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    /**
     * 更新快递员
     *
     * @param id      快递员id
     * @param fastMan 新快递员
     * @return
     */
    @Override
    public int updateFastMan(int id, FastMan fastMan) {
        String sql = "update fastman set username=?,sysphone=?,idcard=?,password=? where id=?";
        List param = new ArrayList();
        param.add(fastMan.getUsername());
        param.add(fastMan.getSysPhone());
        param.add(fastMan.getIdCard());
        param.add(fastMan.getPassword());
        param.add(fastMan.getId());
        int update = 0;
        try {
            update = update(sql, param);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }
    /**
     * 设置登录时间
     * @param id 登录的快递员id
     * @return
     */
    public int setLoginTime(int id){
        String sql  = "update fastman set logintime=? where id = ?";
        List list = new ArrayList();
        list.add(new Date());
        list.add(id);
        int update = 0;
        try {
            update = update(sql, list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }
}
